package com.boco.blch.analysiser.compliance.HW.manager;

import com.boco.blch.analysiser.compliance.ComplianceResolved;

/**
 * YARN&MR日志记录完整性安全基线要求项(控制节点+数据节点)【控制节点】+【数据节点】
 * 
 * 【控制节点】
 * 1、查看 日志文件配置
 * /opt/huawei/bigdata/etc/2_21_ResourceManager/log4j.properties的如下配置中 ，是否存在INFO字段：
 * mapred.audit.logger=INFO,NullAppender
 * log4j.logger.org.apache.hadoop.mapred.AuditLogger=${mapred.audit.logger}或
 * log4j.logger.org.apache.hadoop.mapred.AuditLogger=INFO,audit
 * 
 * 【数据节点】
 * 1、查看 日志文件配置
 * /opt/huawei/bigdata/etc/2_21_ResourceManager/log4j.properties的如下配置中 ，是否存在INFO字段：
 * mapred.audit.logger=INFO,NullAppender
 * log4j.logger.org.apache.hadoop.mapred.AuditLogger=${mapred.audit.logger}或
 * log4j.logger.org.apache.hadoop.mapred.AuditLogger=INFO,audit
 * @author admin
 *
 */
public class BOCO_Yarn_LogAudit_LogRecordComplete implements ComplianceResolved {
	
	private final static String MAPRED_AUDIT_LOGGER = "mapred.audit.logger=INFO,NullAppender";
	private final static String LOG4J_LOGGER_MAPRED_AUDITLOGGER_1 = "log4j.logger.org.apache.hadoop.mapred.AuditLogger=${mapred.audit.logger}";
	private final static String LOG4J_LOGGER_MAPRED_AUDITLOGGER_2 = "log4j.logger.org.apache.hadoop.mapred.AuditLogger=INFO,audit";

	@Override
	public int realEchoResolved(String echoResult) {
		int result = FAILED;
		if(echoResult != null && echoResult.length() > 0){
			if(echoResult.indexOf("NodeManager-start") != -1){//数据节点
				String strResult = echoResult.substring(echoResult.indexOf("NodeManager-start")+"NodeManager-start".length(), echoResult.indexOf("NodeManager-end")).trim();
				result = getResult(strResult);
			}
			if(result == FAILED && echoResult.indexOf("ResourceManager-start") != -1){//控制节点
				String strResult = echoResult.substring(echoResult.indexOf("ResourceManager-start")+"ResourceManager-start".length(), echoResult.indexOf("ResourceManager-end")).trim();
				result = getResult(strResult);
			}
			if(result == FAILED && echoResult.indexOf("JobHistoryServer-start") != -1){//控制节点
				String strResult = echoResult.substring(echoResult.indexOf("JobHistoryServer-start")+"JobHistoryServer-start".length(), echoResult.indexOf("JobHistoryServer-end")).trim();
				result = getResult(strResult);
			}
		}
		
		return result;
	}
	
	/**
	 * 判断
	 * @param strResult
	 * @return
	 */
	private int getResult(String strResult){
		int result = FAILED;
		if(strResult != null){
			strResult = strResult.replace(" ", "");
		}
		if(strResult!=null && (strResult.contains(MAPRED_AUDIT_LOGGER) && 
				(strResult.contains(LOG4J_LOGGER_MAPRED_AUDITLOGGER_1) 
				|| strResult.contains(LOG4J_LOGGER_MAPRED_AUDITLOGGER_2)))){
			result = SUCCESS;
			
		}
		return result;
	}
}
